﻿using System;
using PairingMatrix.Common.Command;
using PairingMatrix.Model;
using PairingMatrix.Services;

namespace PairingMatrix.ViewModel
{
    public class SettingsViewModel : ViewModelBase, ISettingsViewModel
    {
        private readonly SettingsModel _model;

        public SettingsViewModel(SettingsModel model)
        {
            _model = model;
            _model.PropertyChanged += _model_PropertyChanged;
        }

        void _model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if(IsValidProperty(e.PropertyName))
                OnPropertyChanged(e.PropertyName);
        }

        public string MingleUsername
        {
            get { return _model.MingleUsername; }
            set
            {
                if (_model.MingleUsername == value)
                    return;
                _model.MingleUsername = value;
                OnPropertyChanged("MingleUsername");
            }
        }

        public string MinglePassword
        {
            get { return _model.MinglePassword; }
            set
            {
                if (_model.MinglePassword == value)
                    return;
                _model.MinglePassword = value;
                OnPropertyChanged("MinglePassword");
            }
        }

        public string MingleStoryUrl
        {
            get { return _model.MingleStoryUrl; }
            set
            {
                if (_model.MingleStoryUrl == value)
                    return;
                _model.MingleStoryUrl = value;
                OnPropertyChanged("MingleStoryUrl");
            }
        }

        public bool MingleStoryUrlBringAllCardsFromMingle
        {
            get { return _model.MingleStoryUrlBringAllCardsFromMingle; }
            set
            {
                if (_model.MingleStoryUrlBringAllCardsFromMingle == value)
                    return;
                _model.MingleStoryUrlBringAllCardsFromMingle = value;
                OnPropertyChanged("MingleStoryUrlBringAllCardsFromMingle");
            }
        }

        public string MingleRegressionUrl
        {
            get { return _model.MingleRegressionUrl; }
            set
            {
                if (_model.MingleRegressionUrl == value)
                    return;
                _model.MingleRegressionUrl = value;
                OnPropertyChanged("MingleRegressionUrl");
            }
        }

        public bool MingleRegressionUrlBringAllCardsFromMingle
        {
            get { return _model.MingleRegressionUrlBringAllCardsFromMingle; }
            set
            {
                if (_model.MingleRegressionUrlBringAllCardsFromMingle == value)
                    return;
                _model.MingleRegressionUrlBringAllCardsFromMingle = value;
                OnPropertyChanged("MingleRegressionUrlBringAllCardsFromMingle");
            }
        }

        public string MingleDPSUrl
        {
            get { return _model.MingleDPSUrl; }
            set
            {
                if (_model.MingleDPSUrl == value)
                    return;
                _model.MingleDPSUrl = value;
                OnPropertyChanged("MingleDPSUrl");
            }
        }

        public bool MingleDPSUrlBringAllCardsFromMingle
        {
            get { return _model.MingleDPSUrlBringAllCardsFromMingle; }
            set
            {
                if (_model.MingleDPSUrlBringAllCardsFromMingle == value)
                    return;
                _model.MingleDPSUrlBringAllCardsFromMingle = value;
                OnPropertyChanged("MingleDPSUrlBringAllCardsFromMingle");
            }
        }

        private RelayCommand _saveCommand;
        public RelayCommand SaveCommand
        {
            get
            {
                return _saveCommand ??
                       (_saveCommand = new RelayCommand(param => _model.Save()));
            }
        }

        public double Point1MappingToNotUrgent
        {
            get { return _model.Point1MappingToNotUrgent; }
            set
            {
                if (_model.Point1MappingToNotUrgent == value)
                    return;
                _model.Point1MappingToNotUrgent = value;
                OnPropertyChanged("Point1MappingToNotUrgent");
            }
        }

        public double Point1MappingToUrgent
        {
            get { return _model.Point1MappingToUrgent; }
            set
            {
                if (_model.Point1MappingToUrgent == value)
                    return;
                _model.Point1MappingToUrgent = value;
                OnPropertyChanged("Point1MappingToUrgent");
            }
        }

        public bool ShowDaysSpentForProjectCards
        {
            get { return _model.ShowDaysSpentForProjectCards; }
            set
            {
                if (_model.ShowDaysSpentForProjectCards == value)
                    return;
                _model.ShowDaysSpentForProjectCards = value;
                OnPropertyChanged("ShowDaysSpentForProjectCards");
            }
        } 

        public string IterationEndDate
        {
            get { return _model.IterationEndDate.ToShortDateString(); }
            set
            {
                if (_model.IterationEndDate.ToShortDateString() == value)
                    return;
                DateTime date;
                DateTime.TryParse(value, out date);
                if(date != DateTime.MinValue)
                    _model.IterationEndDate = date;
                OnPropertyChanged("IterationEndDate");
            }
        }
    }
}